package cate.game.mail;

import cate.common.game.mail.MailCmd;
import cate.game.mail.po.MailBuilder;
import cp.solution.pattern.Result;
import cp.solution.rpc.RpcMsgHead;
import easy.java.struct.ref.IntRef;

public class MailCmdHandler {

    MailManager parent;
    public MailCmdHandler(MailManager parent) {
        this.parent = parent;
    }

    public Result onRequest(RpcMsgHead head, MailCmd cmd) {
        Result r = new Result();

        MailBuilder mailBuilder = new MailBuilder()
                .setTitle(cmd.title)
                .setContentId(cmd.contentId)
                .setNoticeParamList(cmd.paramList)
                .setRewardConf(cmd.rewardConf)
                .setOperation(cmd.operation)
                .setOperationSub(cmd.operationSub);

        if(cmd.roleMode == MailCmd.ROLE_MODE_LIST) {
            for(String roleId : cmd.roleIdList) {
                parent.send(roleId, mailBuilder.build());
            }
            r.em = "指定的这" + cmd.roleIdList.size() + "个玩家已经发送成功了";
        } else if(cmd.roleMode == MailCmd.ROLE_MODE_ONLINE) {
            IntRef n = new IntRef(0);
            parent.getGame().role.activeForOnline(role -> {
                role.getMail().add(mailBuilder.build());
                n.v++;
            });
            r.em = "发送给了"+n.v+"个在线玩家";
        } else if(cmd.roleMode == MailCmd.ROLE_MODE_ALL) {
            return r.fail("先别用这种模式吧");
        }
        return r.success();
    }
}
